package rexsee.core.alarm;

import rexsee.core.browser.Browser;
import rexsee.core.browser.clazz.JavascriptInterface;
import android.app.KeyguardManager;
import android.app.KeyguardManager.KeyguardLock;
import android.content.Context;

public class RexseeKeyguard implements JavascriptInterface {

	public static final String INTERFACE_NAME = "Keyguard";
	@Override
	public String getInterfaceName() {
		return mBrowser.application.resources.prefix + INTERFACE_NAME;
	}
	@Override
	public JavascriptInterface getInheritInterface(Browser childBrowser) {
		return this;
	}
	@Override
	public JavascriptInterface getNewInterface(Browser childBrowser) {
		return new RexseeKeyguard(childBrowser);
	}

	private final Context mContext;
	private final Browser mBrowser;
	private KeyguardLock mKeyguardLock = null;

	public RexseeKeyguard(Browser browser) {
		mBrowser = browser;
		mContext = browser.getContext();
	}
	public RexseeKeyguard(Context context) {
		mBrowser = null;
		mContext = context;
	}

	//JavaScript Interface

	public void enable() {
		/*
		try {
			DevicePolicyManager dpm = (DevicePolicyManager) mContext.getSystemService(Context.DEVICE_POLICY_SERVICE);
			dpm.lockNow();
		} catch (Exception e) {
			mBrowser.exception(getInterfaceName(), e);
		}
		*/
	}
	public void reEnable() {
		if (mKeyguardLock != null) {
			mKeyguardLock.reenableKeyguard();
			mKeyguardLock = null;
		}
	}
	public void disable() {
		KeyguardManager keyguardManager = (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
		mKeyguardLock = keyguardManager.newKeyguardLock("");
		mKeyguardLock.disableKeyguard();
	}

}
